<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ci_docs_router extends Controller {

	function ci_docs_router() {
		parent::Controller();
		// load the required extras
		$this->load->library('docs');
		$this->load->library('toc');
		$this->load->library('themes');
		$this->load->helper('url');
		$this->load->helper('language');
		
		// load up the guide
		$this->docs->load_guide();
	}
	
	/**
	 * index (  )
	 * 
	 * Basic index function routes directly to index file. Nothing fancy.
	 * 
	 * @access public
	 * @return void
	 */
	function index()
	{
		$data = $this->docs->get_all_vars();
		$this->themes->page('index')->load($data);
	}
	       
	
	/**
	 * route ( $guide_uri, $category = null, $topic = null, $version = null )
	 * 
	 * Routes to the correct files to include
	 * 
	 * @access public
	 * @param string $guide_uri
	 * @param string $category (default: null)
	 * @param string $topic (default: null)
	 * @param string $version (default: null)
	 * @return void
	 */
	function route($guide_uri, $version = null, $category = null, $topic = null) {	
		$data = $this->_create_array($guide_uri, $version, $category, $topic);
		
		// version router gets version 
		// !version numbers are now required
		//if ($this->docs->config('version_support')) {
			$data = $this->_version_router($data);
		//}

		// if table of contents
		if ($this->toc->is_toc()) {
			$data = $this->_toc_router($data);
		}
		// we're not using cat homepages OR we're not at a cat homepage
		else {
			$data = $this->_topic_router($data);
		}
		
		// match up to toc and if url doesnt exist then error out
		if (
			// url not in toc
			!in_array($data['url'] . suffix, $this->toc->category_items['url'])
			// not the toc
			AND $data['topic'] != 'index'
			)
		{
			
			show_error('The topic url [ ' . $data['url'] . suffix . ' ] simply doesn\'t exist.');
		}
		
		$file_url = $data['file_path'];
		$data = $this->docs->get_all_vars();
		//die($file_url);
		$this->themes->page($file_url)->load($data);
	}
	
	
	/**
	 * _create_array ( $guide_uri, $category, $topic, $version )
	 * 
	 * Creates the array used to hold route data
	 * 
	 * @access private
	 * @param string $guide_uri
	 * @param string $category
	 * @param string $topic
	 * @param string $version
	 * @return array
	 */
	function _create_array($guide, $version, $category, $topic) {
		$data = array(
			'guide' => $guide,
			'version' => $version,
			'category' => $category,
			'topic' => $topic,
			'file_path' => trim($this->_generate_url(null, $version, $category, $topic), ds),
			'url' => $this->_generate_url($guide, $version, $category, $topic)
		);
		return $data;
	}
	
	
	/**
	 * _version_router ( $data )
	 * 
	 * If a version is present in the url then use that version, otherwise we will use whats claimed in the config
	 * 
	 * @access private
	 * @param array $data
	 * @return array
	 */
	function _version_router($data) {
		extract($data);
		// version number is present
		if (preg_match('/^v([0-9])/', $category)) {
			$tmp = $version;
			$version = substr($category, 1);
			$category = $topic;
			$topic = $tmp;
		}
		// no version number is present
		else {
			$tmp = $topic;
			$topic = $category;
			$category = $version;
			// version is latest version
			$version = $this->docs->config('guide_latest_version');
		}
		
		// set the new guide version
		$this->docs->config('guide_version', $version);
		
		// if we are using the latest version, unset it for proper urls
		if ($version == $this->docs->config('guide_latest_version')) {
			$version = null;
		}
		// otherwise, regenerate the toc
		else {
			$this->toc->regenerate();
		}
		
		return $this->_create_array($guide, $version, $category, $topic);
	}
	
	
	/**
	 * _toc_router ( $data )
	 * 
	 * Routes to the table of contents page
	 * 
	 * @access private
	 * @param array $data
	 * @return array
	 */
	function _toc_router($data) {
		extract($data);
		$topic = $category;
		$category = null;
		
		return $this->_create_array($guide, $version, $category, $topic);
	}

	
	/**
	 * _topic_router ( $data )
	 * 
	 * Routes to the correct topic
	 * 
	 * @access private
	 * @param array $data
	 * @return array
	 */
	function _topic_router($data) {
		extract($data);
		$topic = isset($topic) ? $topic : 'index';
		
		return $this->_create_array($guide, $version, $category, $topic);
	}
	
	
	function _generate_url($guide, $version, $category, $topic) {
		$url = ds;
		
		foreach (array($guide, $version, $category, $topic) as $segment)
			$url .= (!is_null($segment)) ? $segment . ds : blank;
		
		return rtrim($url, ds);
	}

}

/* End of file ci_docs_router.php */
/* Location: ./app/controllers/ci_docs_router.php */